/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package cn.com.recloud.bbs.service.impl;

import cn.com.recloud.bbs.constant.MetaOptionConstant;
import cn.com.recloud.bbs.entity.bbs_post_comment_log;
import cn.com.recloud.bbs.dao.PostCommentLogDao;
import cn.com.recloud.bbs.entity.metaoption;
import cn.com.recloud.bbs.service.MetaoptionService;
import cn.com.recloud.bbs.service.PostCommentLogService;
import cn.com.recloud.bbs.util.UserUtil;
import cn.com.recloud.bbs.vo.AppCommentVO;
import cn.com.recloud.bbs.vo.CommentVO;
import cn.com.recloud.one.spring.exception.OneException;
import cn.com.recloud.one.spring.service.impl.BaseServiceImpl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author keblea xie
 * @date 2020-06-15
 */
@Service
@Api(tags = "评论的操作")
public class PostCommentLogServiceImpl extends BaseServiceImpl<PostCommentLogDao, bbs_post_comment_log> implements PostCommentLogService {
    @Resource
    PostCommentLogDao postCommentLogDao;
    @Autowired
    private MetaoptionService metaoptionService;
    @Autowired
    private UserUtil userUtil;

    /**
     * 获取评论详情
     * @param id
     * @return
     */
    @Override
    public CommentVO getCommentById(String id) {
        return postCommentLogDao.getCommentById(id);
    }

    /**
     * pc端删除评论
     * @param list
     * @return
     */
    @Override
    public boolean mulDelComment(List<String> list) {
        //根据传入的评论id查询当前评论的子评论
        List<bbs_post_comment_log> commentLogs = postCommentLogDao.findCommentByPidList(list);
        List<String> sonIdList = commentLogs.stream().map(bbs_post_comment_log::getBbs_post_comment_logid).collect(Collectors.toList());
        //将id合并去重
        List<String> idList = Stream.of(list, sonIdList)
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        return super.removeByIds(idList);
    }

    /**
     * app端获取评论列表
     * @param id
     * @return
     */

    @Override
    public List<AppCommentVO> appListComment(String id) {
        if(ObjectUtil.isEmpty(id)) throw new OneException("文章id不能未空！");

        List<AppCommentVO> commentVOS = postCommentLogDao.getAppListById(id);


        if(CollUtil.isEmpty(commentVOS)) return new ArrayList<>();
        List<AppCommentVO> commentParents = commentVOS.stream().filter(comment -> ObjectUtil.isEmpty(comment.getParentId())).collect(Collectors.toList());
        List<AppCommentVO> comments = commentVOS.stream().filter(comment -> ObjectUtil.isNotEmpty(comment.getParentId())).collect(Collectors.toList());
        for (AppCommentVO commentParent:commentParents){
            for (AppCommentVO comment:comments ){
                if(commentParent.getCommentId().equals(comment.getParentId())){
                    List<AppCommentVO> logs = commentParent.getComments();
                    logs.add(comment);
                    commentParent.setComments(logs);
                }
            }
        }
        return commentParents;
    }

    /**
     * 保存评论
     * @param entity
     * @return
     */
    @Override
    public boolean save(bbs_post_comment_log entity) {
        entity.setComment_by_id(userUtil.getUserId());
        entity.setComment_by_name(userUtil.getUserName());
        metaoption option = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.USER_SOURCE, "10");
        entity.setComment_by_type(option.getValue());
        entity.setComment_by_typename(option.getName());
        entity.setObject_type_name(MetaOptionConstant.ARTICAL_ENTITY_NAME);
        option = metaoptionService.findMetaOptionBySetNameAndValue(MetaOptionConstant.POST_COMMENT_STATUS, "2");
        entity.setBbs_post_comment_log_status(option.getValue());
        entity.setBbs_post_comment_log_statusname(option.getName());
        return postCommentLogDao.insert(entity)>=1;
    }

    /**
     * 通过级联删除评论
     */
    @Override
    public boolean removeById(Serializable id) {
        //级联删除
        List<bbs_post_comment_log> commentLogs = postCommentLogDao.selectList(new QueryWrapper<bbs_post_comment_log>().lambda().
                eq(bbs_post_comment_log::getParent_id, id));
        if(commentLogs.size()>0){
            List<String> ids =new ArrayList();
            for (bbs_post_comment_log comment:commentLogs){
                ids.add(comment.getBbs_post_comment_logid());
            }
            postCommentLogDao.deleteBatchIds(ids);
        }
        return postCommentLogDao.deleteById(id)>0;
    }
}
